<?php
/**
 * Created by PhpStorm.
 * User: mars
 * Date: 2019/1/21
 * Time: 9:57 AM
 */

namespace api\controllers;

use api\models\Image;
use common\helpers\Util;
use Guzzle\Http\Exception\ServerErrorResponseException;
use Yii;
use yii\helpers\Url;

class ImageController extends BaseController
{
    public $modelClass = "api\models\Image";

    public function actions()
    {
        $actions = parent::actions();
//        unset($actions['create']);

        return $actions;
    }

    public function actionCreate()
    {
        $model = new Image();
        $model->load(\Yii::$app->getRequest()->post(),'');
        return [
            'request' => \Yii::$app->getRequest()->post(),
            'model'=>$model,
            'file'=>$_FILES,
        ];
    }

    public function actionAvatar(){
        $model = new Image();
        $model->load(Yii::$app->getRequest()->getBodyParams(), '');
        if ($model->save()) {
            $response = Yii::$app->getResponse();
            $response->setStatusCode(201);
            $id = implode(',', array_values($model->getPrimaryKey(true)));
            $response->getHeaders()->set('Location', Url::toRoute(['view', 'id' => $id], true));
        } elseif (!$model->hasErrors()) {
            throw new ServerErrorHttpException('Failed to create the object for unknown reason.');
        }
        return $model;
    }




}